package com.uyayo.system.monitor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.monitor.entity.SysEvent;
import com.uyayo.system.monitor.entity.SysSql;
import com.uyayo.system.monitor.mapper.SysSqlMapper;
import com.uyayo.system.monitor.model.SysSqlCondition;
import com.uyayo.system.monitor.service.SqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("sqlService")
public class SqlServiceImpl implements SqlService {

	private final SysSqlMapper sqlMapper;

	@Autowired
	public SqlServiceImpl(SysSqlMapper sqlMapper) {
		this.sqlMapper = sqlMapper;
	}

	@Override
	public SysSql getSqlMonitorById(String id) throws Exception{
		return sqlMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public PageBean<SysSql> selectSqlMonitor(Integer pageCurrent, Integer pageSize,String sort,String order, SysSqlCondition sysSqlCondition){
		List<SysSql> list;
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		
		Example example = new Example(SysSql.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysSqlCondition.getType())){
			criteria.andEqualTo("type",sysSqlCondition.getType());
		}
		if(!StringUtils.isEmpty(sysSqlCondition.getDescription())){
			criteria.andLike("description","%"+sysSqlCondition.getDescription()+"%");
		}
		if(sysSqlCondition.getBeginStartTime()!=null || sysSqlCondition.getEndStartTime()!=null){
			criteria.andBetween("startTime", sysSqlCondition.getBeginStartTime(), sysSqlCondition.getEndStartTime());
		}
		list = sqlMapper.selectByExample(example);
		//取记录总条数
		PageInfo<SysSql> pageInfo = new PageInfo<>(list);
			
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, list);
	}


	@Override
	public int deleteSqlMonitor(String ids) {
		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (String anIdArr : idArr) {
				if (!anIdArr.equals("")) {
					sqlMapper.deleteByPrimaryKey(anIdArr);
				}
			}
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public List<SysSql> selectSqlMonitor(SysSqlCondition sysSqlCondition) {
		Example example = new Example(SysEvent.class);
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysSqlCondition.getDescription())){
			criteria.andLike("description","%"+sysSqlCondition.getDescription()+"%");
		}
		if(sysSqlCondition.getBeginStartTime()!=null || sysSqlCondition.getEndStartTime()!=null){
			criteria.andBetween("startTime", sysSqlCondition.getBeginStartTime(), sysSqlCondition.getEndStartTime());
		}
		return sqlMapper.selectByExample(example);
	}
	
}

